using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Runtime.Serialization;

namespace TemplateWeb.DAO
{
	#region using
	using System.Data;
	using TemplateWeb.DTO;
	using TemplateWeb.Helpers;
	#endregion

	/// <summary>
	/// Data access object, which get data of PhienDauGia table from database
	/// </summary>
	public class PhienDauGiaDAO : AbstractDAO
	{
		public PhienDauGiaDAO(string strPath): base(strPath)
		{
		}

		public DataSet SelectAll()
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_PhienDauGia_SelectAll";
				base.FillDataSet(ref ds, "PhienDauGia");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByID(int id)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_PhienDauGia_SelectByID";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				base.FillDataSet(ref ds, "PhienDauGia");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public bool Insert(PhienDauGiaDTO obj)
		{
			try
			{
				base.sql = "usp_PhienDauGia_Insert";
				base.InitializeCommand();
				base.AddParameter("@IDHinhThucDauGia", SqlDbType.Int, 1000, obj.IDHinhThucDauGia);
				base.AddParameter("@IDSanPham", SqlDbType.Int, 1000, obj.IDSanPham);
				base.AddParameter("@IDNguoiDang", SqlDbType.UniqueIdentifier, 1000, obj.IDNguoiDang);
				base.AddParameter("@ThoiGianBatDau", SqlDbType.DateTime, 1000, obj.ThoiGianBatDau);
				base.AddParameter("@ThoiGianKetThuc", SqlDbType.DateTime, 1000, obj.ThoiGianKetThuc);
				base.AddParameter("@GiaKhoiDiem", SqlDbType.BigInt, 1000, obj.GiaKhoiDiem);
				base.AddParameter("@GiaToiThieu", SqlDbType.BigInt, 1000, obj.GiaToiThieu);
				base.AddParameter("@TienDatCoc", SqlDbType.BigInt, 1000, obj.TienDatCoc);
				base.AddParameter("@KetQua", SqlDbType.NVarChar, 1000, obj.KetQua);
				base.AddParameter("@XuatHienOTrangChu", SqlDbType.Bit, 1000, obj.XuatHienOTrangChu);
				base.AddParameter("@Duyet", SqlDbType.Bit, 1000, obj.Duyet);
                
                base.AddParameterWithDirection("@ID", SqlDbType.Int, 1000);

                var kq = ExecuteStoredProcedure();
                obj.ID = int.Parse(base.GetParamValue("@ID").ToString());

                return kq;
			}
			catch
			{
				return false;
			}
		}

		public bool Update(PhienDauGiaDTO obj)
		{
			try
			{
				base.sql = "usp_PhienDauGia_Update";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, obj.ID);
				base.AddParameter("@IDHinhThucDauGia", SqlDbType.Int, 1000, obj.IDHinhThucDauGia);
                base.AddParameter("@IDSanPham", SqlDbType.Int, 1000, obj.IDSanPham);
				base.AddParameter("@IDNguoiDang", SqlDbType.UniqueIdentifier, 1000, obj.IDNguoiDang);
				base.AddParameter("@ThoiGianBatDau", SqlDbType.DateTime, 1000, obj.ThoiGianBatDau);
				base.AddParameter("@ThoiGianKetThuc", SqlDbType.DateTime, 1000, obj.ThoiGianKetThuc);
				base.AddParameter("@GiaKhoiDiem", SqlDbType.BigInt, 1000, obj.GiaKhoiDiem);
				base.AddParameter("@GiaToiThieu", SqlDbType.BigInt, 1000, obj.GiaToiThieu);
				base.AddParameter("@TienDatCoc", SqlDbType.BigInt, 1000, obj.TienDatCoc);
				base.AddParameter("@KetQua", SqlDbType.NVarChar, 1000, obj.KetQua);
				base.AddParameter("@XuatHienOTrangChu", SqlDbType.Bit, 1000, obj.XuatHienOTrangChu);
				base.AddParameter("@Duyet", SqlDbType.Bit, 1000, obj.Duyet);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				base.sql = "usp_PhienDauGia_Delete";
				base.InitializeCommand();
				base.AddParameter("@ID", SqlDbType.Int, 1000, id);
				return ExecuteStoredProcedure();
			}
			catch
			{
				return false;
			}
		}

		public DataSet SelectByIDHinhThucDauGia(int idhinhthucdaugia)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_PhienDauGia_SelectByIDHinhThucDauGia";
				base.InitializeCommand();
				base.AddParameter("@IDHinhThucDauGia", SqlDbType.Int, 1000, idhinhthucdaugia);
				base.FillDataSet(ref ds, "PhienDauGia");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

		public DataSet SelectByIDDanhMucCon(int iddanhmuccon)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_PhienDauGia_SelectByIDDanhMucCon";
				base.InitializeCommand();
				base.AddParameter("@IDDanhMucCon", SqlDbType.Int, 1000, iddanhmuccon);
				base.FillDataSet(ref ds, "PhienDauGia");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

        public DataSet SelectByIDDanhMucChinh(int iddanhmucchinh)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_PhienDauGia_SelectByIDDanhMucChinh";
                base.InitializeCommand();
                base.AddParameter("@IDDanhMucChinh", SqlDbType.Int, 1000, iddanhmucchinh);
                base.FillDataSet(ref ds, "PhienDauGia");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }

		public DataSet SelectByIDNguoiDang(Guid idnguoidang)
		{
			DataSet ds;
			try
			{
				ds = new DataSet();
				base.sql = "usp_PhienDauGia_SelectByIDNguoiDang";
				base.InitializeCommand();
				base.AddParameter("@IDNguoiDang", SqlDbType.UniqueIdentifier, 1000, idnguoidang);
				base.FillDataSet(ref ds, "PhienDauGia");
				return ds;
			}
			catch(System.Exception ex)
			{
				if (ex.InnerException != null)
				{
					CommonLogging.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					CommonLogging.Logger.Error(ex.Message);
				}
			}
			finally
			{
				base.CleanUpObject();
			}
			return null;
		}

        public DataSet SelectByIDNguoiDauGia(Guid idnguoidaugia)
        {
            DataSet ds;
            try
            {
                ds = new DataSet();
                base.sql = "usp_PhienDauGia_SelectByIDNguoiDauGia";
                base.InitializeCommand();
                base.AddParameter("@IDNguoiDauGia", SqlDbType.UniqueIdentifier, 1000, idnguoidaugia);
                base.FillDataSet(ref ds, "PhienDauGia");
                return ds;
            }
            catch (System.Exception ex)
            {
                if (ex.InnerException != null)
                {
                    CommonLogging.Logger.Error(ex.InnerException.Message);
                }
                else
                {
                    CommonLogging.Logger.Error(ex.Message);
                }
            }
            finally
            {
                base.CleanUpObject();
            }
            return null;
        }
	}
}